package com.one.blocks.rbac.controller.convert;

import com.one.blocks.rbac.controller.convert.worker.RelationDomainConvert;
import com.one.blocks.rbac.controller.convert.worker.TimeConvert;
import com.one.blocks.rbac.controller.vo.SysRoleGrantMenuVo;
import com.one.blocks.rbac.controller.vo.SysRoleMenuVo;
import com.one.blocks.rbac.domain.SysRoleMenu;
import com.one.blocks.security.util.SubjectHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-01 10:06:14
 */
@Mapper(uses = {TimeConvert.class}, imports = {SubjectHelper.class})
public interface SysRoleMenuConvert extends RelationDomainConvert<SysRoleMenu, SysRoleMenuVo> {

    SysRoleMenuConvert INSTANCE = Mappers.getMapper(SysRoleMenuConvert.class);

    /**
     * 数据对象转换
     *
     * @param sysRoleGrantVo 授权参数
     * @return 存储关联数据
     */
    default List<SysRoleMenu> grantVoToEntity(SysRoleGrantMenuVo sysRoleGrantVo) {
        if (CollectionUtils.isEmpty(sysRoleGrantVo.getMenuIds())) {
            return null;
        }
        List<SysRoleMenu> dataToInsert = new ArrayList<>(sysRoleGrantVo.getMenuIds().size());
        for (Integer menuId : sysRoleGrantVo.getMenuIds()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();

            sysRoleMenu.setRoleId(sysRoleGrantVo.getRoleId());
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setCreateBy(SubjectHelper.getSubjectId());
            sysRoleMenu.setCreateTime(SubjectHelper.getAccessTime());

            dataToInsert.add(sysRoleMenu);
        }
        return dataToInsert;
    }
}